package com.gxc.hash;

import java.util.*;

/**
 * 给定一个数组arr，求差值为k的去重数字对。
 *
 */
public class DifferenceK {

    public static void main(String[] args) {
        int[] arr = new int[]{3, 2 , 4 ,6};
        int k = 0;
        List<List<Integer>> lists = numberPair(arr, k);
        for (List<Integer> list : lists) {
            for (Integer integer : list) {
                System.out.print(integer + "  ");
            }
            System.out.println("");
        }

        System.out.println("--------------");
        k = 2;
        lists = numberPair(arr, k);
        for (List<Integer> list : lists) {
            for (Integer integer : list) {
                System.out.print(integer + "  ");
            }
            System.out.println("");
        }
    }

    public static List<List<Integer>> numberPair(int[] arr, int k) {
        Set<Integer> numberSet = new HashSet<>();
        for (int i = 0; i < arr.length; i++) {
            numberSet.add(arr[i]);
        }

        List<List<Integer>> list = new ArrayList<>();
        for (Integer number : numberSet) {
            if (numberSet.contains(number + k)) {
                list.add(Arrays.asList(number, number + k));
            }
        }
        return list;
    }
}
